package greed;

/**
 * @Classname : CanPlaceFlowers
 * @Description : 605. 种花问题
 * https://leetcode.cn/problems/can-place-flowers/
 * @Author : chentianyu
 * @Date 2023/2/24 00:08
 */


public class CanPlaceFlowers {
    public boolean canPlaceFlowers(int[] flowerbed, int n) {
        if (n == 0) return true;
        int cur = 0;
        for (int i=0; i<flowerbed.length; i++) {
            if (flowerbed[i] == 0 &&
                    (i-1 < 0 || flowerbed[i-1] == 0) &&
                    (i+1 == flowerbed.length || flowerbed[i+1] == 0)) {
                flowerbed[i] = 1;
                if (++cur >= n) return true;
            }
        }
        return false;
    }
}
